// ==UserScript==
// @name         米黄Trintech脚本1.2.3
// @namespace    https://greasyfork.org/en/scripts/408587
// @include      *://*cadency.trintech.com/Certification/JEPrep/*
// @version      1.2.3
// @description  快速填写JE所需信息。
// @author       JunYi
// @match        https://rockwell.cadency.trintech.com/
// @grant        none
// ==/UserScript==

// @require    https://code.jquery.com/jquery-3.5.1.min.js

javascript: (function() {

    // CT根据借贷金额判断
    // 设置 - 颜色，保存移动位置，设置CC，提示框消失时间，填写清空是否提示
    // 可隐藏
    // 辅助功能 - 调整Sheet宽度
    // 自动以提示框，确认框
    // 单元格自动更新时闪一下颜色

    // 自动填写后弹个提示
    // 让居中的框动态居中
    // 和自己的currency不同时提示

    // 自动填写优化：有内容就不要填写了
    // 填写了的格子闪个颜色

    // 调整了脚本<h1>标签英文字母全是大写的问题。
    // BUG修复：修复了一个重置后，内存中的currency不变，导致填写的currency错误的问题。
    // BUG修复：修复了companyCode填写5018会出现错误的问题（currencyMapping5018错写成了5017）。
    // 设置面板优化：优化了跟随框的初始位置。刷新页面第一次打开设置面板时，跟随框不会从下至上一闪而过了。

    var version = '1.2.3';
    var mName = '米黄Trintech脚本' + version;
    var lastUpdateDate = '2020/9/1';

    // 从localStorage中获得数据

    var allData = localStorage.getItem('mhData');;
    var dataDic = {}; // 空字典/对象
    loadData();


    var companyCode = getData('companyCode');
    companyCode = companyCode ? companyCode : '5003'; // 默认5003

    var documentType = ''; // 根据是否有Reversal Period Box来自动判断
    var documentCurrency = 'USD'; // 根据conpanyCode自动匹配

    var preparerCostCenter = getData('preparerCostCenter');
    var preparerCostCenterDefault = '89031';
    preparerCostCenter = preparerCostCenter ? preparerCostCenter : preparerCostCenterDefault; // 默认89031

    var longText = '';
    var shortText = longText.substring(0, 16);

    var postingDate = getData('postingDate');
    postingDate = postingDate ? postingDate : '2020/7/31'; // 默认2020/7/31

    var reversalDate = ''; // 根据postingDate自动计算

    var reversalDay = getData('reversalDay');
    var reversalDayDefault = '10';
    reversalDay = reversalDay ? reversalDay : reversalDayDefault; // reversal日默认是第二个月的10号

    var controlTotal = 'CT 1'; // 默认CT1 不储存
    var ownership = 'GFO'; // 默认BFO 不储存

    var mFontFamily = getData('mFontFamily');
    var mFontFamilyDefault = 'Microsoft YaHei';
    mFontFamily = mFontFamily ? mFontFamily : mFontFamilyDefault; // 'Calibri,Microsoft Yahei'

    var mColor = getData('mColor');
    var mColorDefault = '#333333';
    mColor = mColor ? mColor : mColorDefault; // '#005261'

    // 设置相关数据
    var mTop = getData('mTop');
    var mTopDefault = '260px';
    mTop = mTop ? mTop : mTopDefault; // 默认顶部距离

    var mRight = getData('mRight');
    var mRightDefault = '50px';
    mRight = mRight ? mRight : mRightDefault; // 默认靠右距离

    var mMsgDisappearTime = getData('mMsgDisappearTime');
    var mMsgDisappearTimeDefault = 2;
    mMsgDisappearTime = mMsgDisappearTime ? mMsgDisappearTime : mMsgDisappearTimeDefault; // 默认2秒
    var mWaitTime = mMsgDisappearTime;

    var autoInput = getData('autoInput'); // 自动填写信息
    autoInput = autoInput ? autoInput : false; // 默认false

    // 错误数量
    var mError = 0;


    var currencyMapping = {
        '0016': 'JPY',
        '0135': 'VND',
        '0347': 'CNY',
        '5021': 'CNY',
        '5002': 'CNY',
        '5000': 'AUD',
        '5003': 'CNY',
        '5004': 'CNY',
        '5006': 'CNY',
        '5007': 'HKD',
        '5008': 'INR',
        '5010': 'KRW',
        '5011': 'MYR',
        '5012': 'NZD',
        '5013': 'PHP',
        '5014': 'USD',
        '5015': 'USD',
        '5016': 'TWD',
        '5017': 'THB',
        '5018': 'USD',
        '5019': 'IDR',
        '5023': 'CNY',
    };

    var ctMapping = {
        'CT 1': 'Less than 1M USD',
        'CT 2': '1M to 2M USD',
        'CT 3': 'More than 2M USD'
    };


    // 各种盒子 - SAP
    var reversalPeriodBox = $("#ReversalPeriod");
    var nameBox = $("#Name");

    var companyCodeBox = $("#baseheader_d4137e0c-a437-49a6-8b2c-0a0e6b09e3ee");
    var documentTypeBox = $("#detailheader_ecde07c7-5d82-4406-a222-a4d0010f574c");
    var documentCurrencyBox = $("#detailheader_4f54f35e-c0ff-4a94-83ac-a46e0112442c");
    var preparerCostCenterBox = $("#detailheader_52d955e7-ca17-4689-9270-a4f400db31ee");
    var ownershipBox = $("#detailheader_377b988a-42a7-4a2e-ad80-a4f700a59a0a"); // readonly 
    var referenceBox = $("#detailheader_69234d49-9a22-4e1a-81b9-a4df00f02bc1");
    var adHocJudgmentalBox = $("#detailheader_4a1398cf-1004-4929-9f09-a4d001128f5b");

    var postingDateBox = $("#baseheader_e954c74e-f1c5-4b45-89fe-a46e01124419"); // SAP和non-SAP共用
    var documentDateBox = $("#detailheader_fbd595aa-4570-4d7f-a6df-a4d00112acc6");
    var translationDateBox = $("#detailheader_12b345f4-e5fe-40f8-aa43-a4df00efe030");
    var reversalDateBox = $("#baseheader_2bd855a7-6d60-41cc-8333-a46e0112442c");
    var controlTotalBox = $("#detailheader_d1f641fd-05d3-4ec7-b868-a4fb009d8d84");
    var ctDescriptionBox = $("#detailheader_aea737c3-cf63-4f16-9c15-a50f009de668"); // readonly 
    var jeDescriptionBox = $("#detailheader_50993ab5-3add-4924-8f8a-a4df00faebb1"); // SAP和non-SAP共用

    // 各种盒子 - NonSAP
    var nonSAPLocationBox = $("#detailheader_02fb3f29-8c29-412d-a656-a71b00edf360");
    var entryCurrencyBox = $("#detailheader_6ca1d8e1-c1a1-4915-8ea6-a71b00ff1443");
    var documentNoBox = $("#detailheader_4e5c54ab-23d4-4304-ae9a-a71c00d73f19");




    // 插件UI
    var prepareArea = $("#PrepWrapper");
    // var prepareArea = document.querySelector('#PrepWrapper');

    // 主界面
    var mBox = $("<div></div>"); // 装所有按钮的大盒子
    prepareArea.append(mBox);
    mBox.css({
        backgroundColor: "#fff",
        position: "absolute",
        height: 270,
        width: 400,
        right: mRight,
        top: mTop,
        borderRadius: 25,
        boxShadow: "0px 0px 5px #333",
        zIndex: 9999999,
        fontFamily: mFontFamily,
    });


    // 提示框
    var msgBox = $("<div></div>");
    msgBox.addClass("msg");
    prepareArea.append(msgBox); // 先添加（透明度0）
    msgBox.css({
        height: 70,
        width: "auto",
        userSelect: "none", // 文字不能选中
    });


    var msgIcon = document.createElement('div'); // 错误正确的小圆圈
    msgIcon.className = 'icon iconCorrect';
    msgBox.append(msgIcon);

    var msg = document.createElement('span');
    msgBox.append(msg);
    msg.style.marginLeft = '10px';
    msg.style.marginRight = '50px';
    msg.style.height = '70px';
    msg.style.lineHeight = '70px';


    // 确认框
    var cover = document.createElement('div');
    cover.className = 'cover';
    prepareArea.append(cover);
    cover.style.position = 'absolute';
    cover.style.display = 'none'; // 刚开始消失
    cover.style.top = '0';
    cover.style.left = '0';
    cover.style.height = '1200px';
    cover.style.width = '100%';
    cover.style.backgroundColor = 'black';
    cover.style.opacity = '0';
    cover.style.zIndex = '9999999';

    var confirmBox = $("<div></div>");
    // var confirmBox = document.createElement('div');
    confirmBox.addClass("msg cfmBox");
    // confirmBox.className = 'msg cfmBox';
    prepareArea.append(confirmBox); //  先添加（透明度0）
    confirmBox.css({
        display: "none",
        height: 120,
        userSelect: "none",
    });

    var confirmIcon = document.createElement('div'); // 错误正确的小圆圈
    confirmIcon.className = 'icon iconCorrect';
    confirmBox.append(confirmIcon);

    var confirmMsg = document.createElement('div');
    confirmBox.append(confirmMsg);
    confirmMsg.style.display = 'inline-block';
    confirmMsg.style.marginLeft = '10px';
    confirmMsg.style.marginRight = '100px';
    confirmMsg.style.textAlign = 'center';
    confirmMsg.style.height = '120px';
    confirmMsg.style.lineHeight = '120px';
    // confirmMsg.style.margin = '0 auto';

    var doWhat = ''; // foncirm做什么

    var btnConfirm = document.createElement('div');
    btnConfirm.className = 'mBtn';
    confirmBox.append(btnConfirm);
    btnConfirm.style.right = '90px';
    btnConfirm.innerHTML = '确认';
    btnConfirm.addEventListener('click', function() {
        var newTop = parseInt($(".cfmBox").css("top")) - 20;
        $(".cfmBox").animate({
            top: newTop + "px",
            opacity: 0,
        }, 200, "swing", function() {
            $(".cfmBox").css({
                top: newTop + 20 * 2,
                display: "none",
            });
            cover.style.display = 'none';
            if (doWhat == '填写') {
                autoFill();
                showMsg("自动填写完成。", true);
            } else if (doWhat == '清空') {
                autoClear();
                showMsg("自动清空完成。", true);
            };
        });
    })

    var btnCancel = document.createElement('div');
    btnCancel.className = 'mBtn';
    confirmBox.append(btnCancel);
    btnCancel.style.right = '30px';
    btnCancel.innerHTML = '取消';
    btnCancel.addEventListener('click', function() {
        var newTop = parseInt($(".cfmBox").css("top")) - 20;
        $(".cfmBox").animate({
            top: newTop + "px",
            opacity: 0,
        }, 200, "swing", function() {
            $(".cfmBox").css({
                top: newTop + 20 * 2,
                display: "none",
            });
            cover.style.display = 'none';
        });
    })


    // 更新提示框
    var versionBox = $("<div></div>");
    versionBox.addClass("msg versionBox");
    prepareArea.append(versionBox);
    versionBox.css({
        position: "fixed",
        height: 600,
        width: 1150,
        userSelect: "none", // 文字不能选中
    });
    var versionTitleBox = $("<div></div>");
    versionBox.append(versionTitleBox);
    versionTitleBox.css({
        position: "relative",
        backgroundColor: mColor,
        height: 30,
        width: "100%",
        borderRadius: "23px 23px 1px 1px",
        margin: 0,
        boxShadow: "0px 1px 8px #333",
    });
    var versionTitle = $("<h1></h1>");
    versionTitleBox.append(versionTitle);
    versionTitle.html("版本信息");
    versionTitle.css({
        display: "inline-block",
        fontSize: 12,
        textAlign: "center",
        height: 30,
        lineHeight: "30px",
        width: "100%",
        fontFamily: mFontFamily,
        color: "#fff", //  "#fff"
        margin: "0",
    });
    var versionCloseBtn = $("<span></span>");
    versionTitleBox.append(versionCloseBtn);
    versionCloseBtn.html("-");
    versionCloseBtn.css({
        position: "absolute",
        fontSize: 15,
        fontWeight: 700,
        right: 10,
        width: 30,
        textAlign: "center",
        height: 30,
        lineHeight: "30px",
        fontFamily: mFontFamily,
        color: "#fff", //  "#fff"
        margin: 0,
        cursor: "pointer",
    });
    versionCloseBtn.mouseover(function() {
        versionCloseBtn.css("color", "#54565a"); // 灰色
    });
    versionCloseBtn.mouseleave(function() {
        versionCloseBtn.css("color", "#fff");
    });
    versionCloseBtn.click(function() {
        versionBox.animate({
            opacity: 0,
            top: (window.innerHeight - 600) / 2 - 20,
        }, 500, "swing", function() { versionBox.css("display", "none"); });
        if (!openInSetting) {
            cover.style.display = 'none'; // 在设置里打开不需要消失
            cover.style.opacity = '0';
        }
    });
    var versionShowArea = $("<div></div>");
    versionBox.append(versionShowArea);
    versionShowArea.css({
        position: "absolute",
        width: parseInt(versionBox.css("width")) - 200,
        height: parseInt(versionBox.css("height")) - 100,
        // backgroundColor: "#ccc",
    });
    versionShowArea.css({
        top: 30 + (parseInt(versionBox.css("height")) - 30 - parseInt(versionShowArea.css("height"))) / 2,
        left: (parseInt(versionBox.css("width")) - parseInt(versionShowArea.css("width"))) / 2,
        overflow: "auto",
    });
    var versionHistortText = $("<p></p>");
    versionShowArea.addClass("versionShowArea");
    versionShowArea.append(versionHistortText);
    versionHistortText.html("");
    versionHistortText.css({
        fontFamily: mFontFamily,
    });
    // var versionLine = $("<span></span>");
    // versionBox.append(versionLine);
    // versionLine.css({
    //     position: "absolute",
    //     left: 249, // 黄金分割比(parseInt(versionBox.css("width")) * (1 - 0.618)) / 2
    //     top: 30 + (parseInt(versionBox.css("height")) - 30 - 450) / 2,
    //     width: 2,
    //     height: 450,
    //     backgroundColor: "#CCC", // 灰色
    //     borderRadius: 2,
    // });
    // var versionHistoryBox = $("<div></div>");
    // versionBox.append(versionHistoryBox);
    // var versionHistoryText = $("<h2></h2>");
    // versionBox.append(versionHistoryText);
    // versionHistoryText.html("历史版本");
    // versionHistoryText.css({
    //     position: "absolute",
    //     display: "inline-block",
    //     fontSize: 20,
    //     width: versionLine.css("left"),
    //     textAlign: "center",
    //     top: versionLine.css("top"),
    //     color: mColor, // 灰色
    //     fontFamily: mFontFamily,
    // });


    // 设置框
    var settingBox = $("<div></div>");
    settingBox.addClass("msg settingBox");
    prepareArea.append(settingBox);
    settingBox.css({
        position: "fixed",
        height: 550,
        width: 900,
        userSelect: "none", // 文字不能选中
    });
    var settingTitleBox = $("<div></div>");
    settingBox.append(settingTitleBox);
    settingTitleBox.css({
        position: "relative",
        backgroundColor: mColor,
        height: 30,
        width: "100%",
        borderRadius: "23px 23px 1px 1px",
        margin: 0,
        boxShadow: "0px 1px 8px #333",
    });
    var settingTitle = $("<h1></h1>");
    settingTitleBox.append(settingTitle);
    settingTitle.html("设置");
    settingTitle.css({
        display: "inline-block",
        fontSize: 12,
        textAlign: "center",
        height: 30,
        lineHeight: "30px",
        width: "100%",
        fontFamily: mFontFamily,
        color: "#fff", //  "#fff"
        margin: "0",
    });
    var settingCloseBtn = $("<span></span>");
    settingBox.append(settingCloseBtn);
    settingCloseBtn.html("-");
    settingCloseBtn.css({
        position: "absolute",
        fontSize: 15,
        fontWeight: 700,
        top: 0,
        right: 10,
        width: 30,
        textAlign: "center",
        height: 30,
        lineHeight: "30px",
        fontFamily: mFontFamily,
        color: "#fff", //  "#fff"
        margin: 0,
        cursor: "pointer",
    });
    settingCloseBtn.mouseover(function() {
        settingCloseBtn.css("color", "#54565a"); // 灰色
    });
    settingCloseBtn.mouseleave(function() {
        settingCloseBtn.css("color", "#fff");
    });
    settingCloseBtn.click(function() {
        settingBox.animate({
            opacity: 0,
            top: (window.innerHeight - 550) / 2 - 20,
        }, 500, "swing", function() { settingBox.css("display", "none"); });
        cover.style.display = 'none';
        cover.style.opacity = '0';
    });
    // 标签选项
    var settingTabBtn_Base = $("<div></div>");
    settingBox.append(settingTabBtn_Base);
    settingTabBtn_Base.addClass("settingTabBtn");
    settingTabBtn_Base.html("基本");
    settingTabBtn_Base.mouseover(function() {
        touchSettingTabBtn(0);
    });
    settingTabBtn_Base.mouseleave(function() {
        leaveSettingTabBtn(0);
    });
    settingTabBtn_Base.click(function() {
        selectSettingTabBtn(0);
    });

    var settingTabBtn_Data = $("<div></div>");
    settingBox.append(settingTabBtn_Data);
    settingTabBtn_Data.addClass("settingTabBtn");
    settingTabBtn_Data.html("数据");
    settingTabBtn_Data.mouseover(function() {
        touchSettingTabBtn(1);
    });
    settingTabBtn_Data.mouseleave(function() {
        leaveSettingTabBtn(1);
    });
    settingTabBtn_Data.click(function() {
        selectSettingTabBtn(1);
    });

    var settingTabBtn_Empty1 = $("<div></div>");
    settingBox.append(settingTabBtn_Empty1);
    settingTabBtn_Empty1.addClass("settingTabBtn");
    settingTabBtn_Empty1.html("");
    var settingTabBtn_Empty2 = $("<div></div>");
    settingBox.append(settingTabBtn_Empty2);
    settingTabBtn_Empty2.addClass("settingTabBtn");
    settingTabBtn_Empty2.html("");
    var settingTabBtn_Empty3 = $("<div></div>");
    settingBox.append(settingTabBtn_Empty3);
    settingTabBtn_Empty3.addClass("settingTabBtn");
    settingTabBtn_Empty3.html("");
    var settingTabBtn_Empty4 = $("<div></div>");
    settingBox.append(settingTabBtn_Empty4);
    settingTabBtn_Empty4.addClass("settingTabBtn");
    settingTabBtn_Empty4.html("");
    var settingTabBtn_Empty5 = $("<div></div>");
    settingBox.append(settingTabBtn_Empty5);
    settingTabBtn_Empty5.addClass("settingTabBtn");
    settingTabBtn_Empty5.html("");
    var settingTabBtn_Empty6 = $("<div></div>");
    settingBox.append(settingTabBtn_Empty6);
    settingTabBtn_Empty6.addClass("settingTabBtn");
    settingTabBtn_Empty6.html("");
    var settingTabBtn_Empty7 = $("<div></div>");
    settingBox.append(settingTabBtn_Empty7);
    settingTabBtn_Empty7.addClass("settingTabBtn");
    settingTabBtn_Empty7.html("");

    var settingTabBtn_About = $("<div></div>");
    settingBox.append(settingTabBtn_About);
    settingTabBtn_About.addClass("settingTabBtn");
    settingTabBtn_About.html("关于");
    settingTabBtn_About.mouseover(function() {
        touchSettingTabBtn(9);
    });
    settingTabBtn_About.mouseleave(function() {
        leaveSettingTabBtn(9);
    });
    settingTabBtn_About.click(function() {
        selectSettingTabBtn(9);
    });

    var settingTabBtn_Float = $("<div></div>"); // 这是做鼠标经过选项时，滑动的小块
    settingBox.append(settingTabBtn_Float);
    settingTabBtn_Float.addClass("settingTabBtn settingTabBtn_Float");

    // 各个设置面板
    // 【基本】面板
    var settingTab_Base = $("<div></div>");
    settingBox.append(settingTab_Base);
    settingTab_Base.addClass("settingTab baseTab");
    var settingTab_Base_ul = $("<ul></ul>");
    settingTab_Base.append(settingTab_Base_ul);
    // 主题颜色
    var settingTab_Base_li_Title1 = $("<li class='tabLi'><span style='font-size:14px;font-weight:700;'>外观</span></li>");
    settingTab_Base_ul.append(settingTab_Base_li_Title1);
    var settingTab_Base_li_Color = $("<li class='tabLi settingTab_Base_li1'>主题颜色&nbsp&nbsp<input type='color' value=" + mColor + " class='colorTab' style='padding:0;border:none;margin:0;outline:none;width:14px;height:18px;cursor:pointer;'></li>");
    settingTab_Base_ul.append(settingTab_Base_li_Color);
    var colorTab = $(".colorTab");
    colorTab.change(function() {
        mColor = colorTab.val()
        setColor();
        addData('mColor', mColor)
    })
    settingTab_Base_li_Color.dblclick(function() {
        mColor = mColorDefault;
        colorTab.val(mColor);
        setColor();
        addData('mColor', mColor)
        showMsg('恢复默认主题颜色：' + mColor, true)
    })

    function setColor() {
        settingTitleBox.css("backgroundColor", mColor)
        mTitleBox.style.backgroundColor = mColor;
        var settingTabBtn = $(".settingTabBtn");
        $(settingTabBtn[selectedTab]).css("backgroundColor", mColor);
        settingTabBtn_Float.css("backgroundColor", mColor);
    }
    // 分隔线
    var settingTab_Base_li_Line1 = $("<li style='list-style:none;height:30px;'></li>");
    // var settingTab_Base_li_Line1 = $("<li class='tabLi' style='padding:15px;'><div style='display:inline-block;height:1px;width:90%;background-color:#ccc;margin-top:14px;'></div></li>");   
    settingTab_Base_ul.append(settingTab_Base_li_Line1);
    // 程序
    var settingTab_Base_li_Title2 = $("<li class='tabLi'><span style='font-size:14px;font-weight:700;'>程序</span></li>");
    settingTab_Base_ul.append(settingTab_Base_li_Title2);
    var settingTab_Base_li_AutoFill = $("<li class='tabLi'><label><input class='autoFill' type='checkbox'>&nbsp加载界面时自从填写</input></label></li>");
    settingTab_Base_ul.append(settingTab_Base_li_AutoFill);
    $(".autoFill").prop("checked", autoInput);
    $(".autoFill").click(function() {
        addData("autoInput", $(this).prop("checked"));
    });

    var settingTab_Empty2 = $("<div></div>");
    settingBox.append(settingTab_Empty2);
    settingTab_Empty2.addClass("settingTab");
    var settingTab_Empty3 = $("<div></div>");
    settingBox.append(settingTab_Empty3);
    settingTab_Empty3.addClass("settingTab");
    var settingTab_Empty4 = $("<div></div>");
    settingBox.append(settingTab_Empty4);
    settingTab_Empty4.addClass("settingTab");
    var settingTab_Empty5 = $("<div></div>");
    settingBox.append(settingTab_Empty5);
    settingTab_Empty5.addClass("settingTab");
    var settingTab_Empty6 = $("<div></div>");
    settingBox.append(settingTab_Empty6);
    settingTab_Empty6.addClass("settingTab");
    var settingTab_Empty7 = $("<div></div>");
    settingBox.append(settingTab_Empty7);
    settingTab_Empty7.addClass("settingTab");
    var settingTab_Empty8 = $("<div></div>");
    settingBox.append(settingTab_Empty8);
    settingTab_Empty8.addClass("settingTab");
    var settingTab_Empty9 = $("<div></div>");
    settingBox.append(settingTab_Empty9);
    settingTab_Empty9.addClass("settingTab");

    var settingTab_About = $("<div></div>");
    settingBox.append(settingTab_About);
    settingTab_About.addClass("settingTab tabAbout");
    settingTab_About.css({
        position: "relative",
    });
    var settingTab_About_ul = $("<ul></ul>");
    settingTab_About.append(settingTab_About_ul);
    var settingTab_About_li1 = $("<li class='tabLi'>脚本名称：" + mName + "</li>");
    var settingTab_About_li2 = $("<li class='tabLi'>当前版本：" + version + "</li>");
    var settingTab_About_li3 = $("<li class='tabLi'>最后更新日期：" + lastUpdateDate + "</li>");
    settingTab_About_ul.append(settingTab_About_li1);
    settingTab_About_ul.append(settingTab_About_li2);
    settingTab_About_ul.append(settingTab_About_li3);

    var tabCkeckUpdate = $("<span class='tabButton'>检查更新</span>");
    var tabCkeckVersion = $("<span class='tabButton'>查看版本信息</span>");
    // 反馈：https://greasyfork.org/en/scripts/408587/feedback
    settingTab_About.append(tabCkeckUpdate);
    settingTab_About.append(tabCkeckVersion);
    $(".tabButton").css({
        position: "absolute",
        display: "inline-block",
        top: 135,
        left: 57,
        width: 100,
        height: 30,
        lineHeight: "30px",
        color: "#333",
        textAlign: "center",
        backgroundColor: "#fff",
        boxShadow: "0 0 3px #333",
        borderRadius: "30px",
        cursor: "pointer",
    });
    tabCkeckVersion.css({
        left: 57 + 100 + 15,
    });

    tabCkeckUpdate.mouseover(function() {
        tabCkeckUpdate.css({
            color: "#fff",
            backgroundColor: "#54565a", // 灰色
        })
    })
    tabCkeckUpdate.mouseleave(function() {
        tabCkeckUpdate.css({
            color: "#333",
            backgroundColor: "#fff",
        })
    })
    tabCkeckUpdate.mousedown(function() {
        tabCkeckUpdate.css({
            color: "#fff",
            backgroundColor: mColor, // 灰色
        })
    })
    tabCkeckUpdate.click(function() {
        window.open("https://greasyfork.org/en/scripts/408587");
    });
    tabCkeckVersion.mouseover(function() {
        tabCkeckVersion.css({
            color: "#fff",
            backgroundColor: "#54565a", // 灰色
        })
    })
    tabCkeckVersion.mouseleave(function() {
        tabCkeckVersion.css({
            color: "#333",
            backgroundColor: "#fff",
        })
    })
    tabCkeckVersion.mousedown(function() {
        tabCkeckVersion.css({
            color: "#fff",
            backgroundColor: mColor, // 灰色
        })
    })
    tabCkeckVersion.click(function() {
        openInSetting = true;
        showVersionBox();
    })







    // 标题框
    var mTitleBox = document.createElement('div');
    mBox.append(mTitleBox);
    mTitleBox.style.backgroundColor = mColor;
    mTitleBox.style.height = '30px';
    mTitleBox.style.width = '100%';
    mTitleBox.style.borderRadius = '25px 25px 1px 1px';
    mTitleBox.style.margin = '0px';
    mTitleBox.style.boxShadow = '0px 1px 8px #333';
    mTitleBox.style.cursor = 'move';

    mTitleBox.addEventListener('mousedown', function(e) {
        // var x = e.pageX - mBox.offsetLeft; // 鼠标距离浏览器左边距离 - 盒子距离浏览器左边距离 = 鼠标距离盒子做边框距离（不变）
        // var y = e.pageY - mBox.offsetTop; // 同上，距离顶部
        var x = e.pageX - parseInt(mBox.css("left"));
        var y = e.pageY - parseInt(mBox.css("top"));
        var mWidth = parseInt(mBox.css("width"));

        document.addEventListener('mousemove', move);

        function move(e) {
            mBox.css({
                right: window.innerWidth - (e.pageX - x) - mWidth,
                top: e.pageY - y,
            });
            // mBox.style.right = window.innerWidth - (e.pageX - x) - mBox.offsetWidth + 'px';
            // mBox.style.top = e.pageY - y + 'px';
        }

        document.addEventListener('mouseup', function() {
            document.removeEventListener('mousemove', move);
        })
    })

    var mLogoBox = document.createElement('div');
    mTitleBox.appendChild(mLogoBox);
    mLogoBox.style.width = '200px';
    mLogoBox.style.height = '30px';
    mLogoBox.style.margin = '0 auto';

    // var mLogo = document.createElement('div');
    // mLogoBox.appendChild(mLogo);
    // mLogo.style.display = 'inline-block';
    // mLogo.style.height = '25px';
    // mLogo.style.lineHeight = '25px';
    // mLogo.style.width = '25px';
    // mLogo.style.textAlign = 'center';
    // mLogo.style.backgroundColor = '#fff';
    // mLogo.style.borderRadius = '15px';
    // mLogo.style.color = mColor;
    // mLogo.innerHTML = 'M';

    var mTitle = document.createElement('h1');
    mLogoBox.appendChild(mTitle);
    mTitle.innerHTML = mName;
    mTitle.style.display = 'inline-block';
    mTitle.style.fontSize = '12px';
    mTitle.style.textAlign = 'center';
    mTitle.style.height = '30px';
    mTitle.style.lineHeight = '30px';
    mTitle.style.width = '100%';
    mTitle.style.fontFamily = mFontFamily;
    mTitle.style.color = '#fff';
    mTitle.style.margin = '0px';
    mTitle.style.userSelect = 'none'; // 文字不能选中
    mTitle.style.textTransform = 'none'; // 文字不大写

    var mUl = document.createElement('ul');
    mBox.append(mUl);



    // 第一个Li - companyCode
    var mLi1 = document.createElement('li');
    mLi1.className = 'mLi';
    mUl.appendChild(mLi1);

    var mText1 = document.createElement('i'); // 字段CompanyCode
    mText1.className = 'mText';
    mLi1.appendChild(mText1);
    mText1.innerHTML = 'CompanyCode ';
    var mCompanyCodeBox = document.createElement('input'); // 单元格
    mCompanyCodeBox.className = 'mInput';
    mLi1.appendChild(mCompanyCodeBox);
    var mCheckCompanyCode = document.createElement('i');
    mCheckCompanyCode.className = 'mText mCheck';
    mLi1.appendChild(mCheckCompanyCode);

    mCompanyCodeBox.addEventListener('blur', function() {
        companyCode = mCompanyCodeBox.value;
        documentCurrency = mapCurrency(mCompanyCodeBox.value);
        mCurrencyBox.value = documentCurrency;
        checkAll();
    })




    // 第二个Li - SB or Z1
    var mLi2 = document.createElement('li');
    mLi2.className = 'mLi';
    mUl.appendChild(mLi2);

    var mDocumentType1 = document.createElement('input');
    mDocumentType1.type = 'radio';
    mDocumentType1.name = 'DocumentType';
    mDocumentType1.innerHTML = 'SB'
    mDocumentType1.style.color = mColor;
    mLi2.appendChild(mDocumentType1);
    var mText2 = document.createElement('i');
    mText2.className = 'mText';
    mLi2.appendChild(mText2);
    mText2.innerHTML = 'SB ';
    var mDocumentType2 = document.createElement('input');
    mDocumentType2.type = 'radio';
    mDocumentType2.name = 'DocumentType';
    mDocumentType2.innerHTML = 'SB'
    mDocumentType2.style.color = mColor;
    mLi2.appendChild(mDocumentType2);
    var mText3 = document.createElement('i');
    mText3.className = 'mText';
    mLi2.appendChild(mText3);
    mText3.innerHTML = 'Z1 ';

    mDocumentType1.addEventListener('click', function() {
        documentType = 'SB';
        selectDocumentType(documentType);
    });

    mDocumentType2.addEventListener('click', function() {
        documentType = 'Z1';
        selectDocumentType(documentType);
    });



    // 第二点五个 Currency
    var mLi25 = document.createElement('li');
    mLi25.className = 'mLi';
    mUl.appendChild(mLi25);

    var mText25 = document.createElement('i');
    mText25.className = 'mText';
    mLi25.appendChild(mText25);
    mText25.innerHTML = 'Currency ';
    var mCurrencyBox = document.createElement('input');
    mCurrencyBox.className = 'mInput';
    mLi25.appendChild(mCurrencyBox);
    var mCheckCurrency = document.createElement('i');
    mCheckCurrency.className = 'mText mCheck';
    mLi25.appendChild(mCheckCurrency);

    mCurrencyBox.addEventListener('blur', function() {
        documentCurrency = mCurrencyBox.value;
        checkAll();
    })






    // 第三个Date
    var mLi3 = document.createElement('li');
    mLi3.className = 'mLi';
    mUl.appendChild(mLi3);

    var mText33 = document.createElement('i');
    mText33.className = 'mText';
    mLi3.appendChild(mText33);
    mText33.innerHTML = 'PostingDate (Y/M/D) ';
    var mPostingDateBox = document.createElement('input');
    mPostingDateBox.className = 'mInput';
    mLi3.appendChild(mPostingDateBox);
    var mCheckPostingDate = document.createElement('i');
    mCheckPostingDate.className = 'mText mCheck';
    mLi3.appendChild(mCheckPostingDate);

    var mLi4 = document.createElement('li');
    mLi4.className = 'mLi';
    mUl.appendChild(mLi4);

    var mText4 = document.createElement('i');
    mText4.className = 'mText';
    mLi4.appendChild(mText4);
    mText4.innerHTML = 'ReversalDate ';
    var mReversalDateBox = document.createElement('input');
    mReversalDateBox.className = 'mInput';
    mLi4.appendChild(mReversalDateBox);
    var mCheckReversalDate = document.createElement('i');
    mCheckReversalDate.className = 'mText mCheck';
    mLi4.appendChild(mCheckReversalDate);

    mPostingDateBox.addEventListener('blur', function() {
        // 修改posting日期后自动保存
        postingDate = mPostingDateBox.value;
        selectDocumentType(documentType);
        checkAll();
    })

    mReversalDateBox.addEventListener('blur', function() {
        reversalDate = mReversalDateBox.value;
        checkAll();
    })



    // 第四个LongText
    var mLi5 = document.createElement('li');
    mLi5.className = 'mLi';
    mUl.appendChild(mLi5);

    var mText5 = document.createElement('i');
    mText5.className = 'mText';
    mLi5.appendChild(mText5);
    mText5.innerHTML = 'LongText ';
    var mLongTextBox = document.createElement('input');
    mLi5.appendChild(mLongTextBox);
    mLongTextBox.style.width = '200px';
    // mLongTextBox.autocomplete = 'false';
    mLongTextBox.setAttribute('autocomplete', 'off')
    mLongTextBox.setAttribute('placeholder', '在这里输入JE Description')
    mLongTextBox.style.float = 'right';
    mLongTextBox.style.marginRight = '50px';
    mLongTextBox.style.fontFamily = mFontFamily;


    // 下边的按钮
    var btnFill = document.createElement('div');
    btnFill.className = 'mBtn';
    mBox.append(btnFill);
    btnFill.style.left = '40px';
    btnFill.innerHTML = '填写';
    btnFill.addEventListener('click', function() {
        // autoFill();
        if (mError == 0) {
            var system = companyCodeBox ? 'SAP' : 'non-SAP';
            showConfirm('检测到当前的Template是：' + system + '，是否自动填写JE？', '填写');
        } else {
            showMsg('不能填写：有' + mError + '处错误。', false, function() {});
        }
    })

    var btnClear = document.createElement('div');
    btnClear.className = 'mBtn';
    mBox.append(btnClear);
    btnClear.style.left = (40 + (50 + 10) * (2 - 1)) + 'px';
    btnClear.innerHTML = '清空';
    btnClear.addEventListener('click', function() {
        var system = companyCodeBox ? 'SAP' : 'non-SAP';
        showConfirm('检测到当前的Template是：' + system + '，是否清空所有内容？', '清空');
        // autoClear();
    })

    var btnReset = document.createElement('div');
    btnReset.className = 'mBtn';
    mBox.append(btnReset);
    btnReset.style.left = (40 + (50 + 10) * (3 - 1)) + 'px';
    btnReset.innerHTML = '重置';
    btnReset.addEventListener('click', function() {
        autoReset();
    })

    var btnSetting = $("<div></div>");
    btnSetting.addClass("mBtn mSetting");
    mBox.append(btnSetting);
    btnSetting.css({
        left: (40 + (50 + 10) * (4 - 1)),
    });
    btnSetting.html("设置");
    btnSetting.click(function() {
        showSetting(0); // 默认显示基本设置
    });
    // $(".mSetting").on("click", function() {
    //     alert(11);
    // });












    // 填写默认值
    // 填写默认值
    // 填写默认值
    // 填写默认值
    // 填写默认值
    companyCode = identifyCompanyCode() ? identifyCompanyCode() : companyCode;
    mCompanyCodeBox.value = companyCode;
    addData('companyCode', companyCode); // 保存数据
    documentType = reversalPeriodBox.length ? 'Z1' : 'SB';
    if (documentType == 'SB') {
        mDocumentType1.setAttribute('checked', 'checked');
        mDocumentType2.disabled = true;
    } else {
        mDocumentType2.setAttribute('checked', 'checked');
        mDocumentType1.disabled = true;
    };
    selectDocumentType(documentType);
    documentCurrency = mapCurrency(companyCode);
    mCurrencyBox.value = documentCurrency;
    mPostingDateBox.value = postingDate;
    mReversalDateBox.value = reversalDate;
    longText = nameBox.val();
    mLongTextBox.value = longText;
    checkAll(); // 检测
    if (autoInput && mError == 0) {
        autoFill(); // 自动填写
    };









    // 统一调整格式
    var mStyleFori = document.getElementsByClassName('mText');
    for (var i = 0; i < mStyleFori.length; i++) {
        mStyleFori[i].style.fontStyle = 'normal';
        mStyleFori[i].style.height = '20px';
        mStyleFori[i].style.lineHeight = '20px';
    }
    var mStyleForli = document.getElementsByClassName('mLi');
    for (var i1 = 0; i1 < mStyleForli.length; i1++) {
        mStyleForli[i1].style.position = 'relative';
        mStyleForli[i1].style.listStyle = 'none';
        mStyleForli[i1].style.marginTop = '10px';
    }
    var mStyleForInput = document.getElementsByClassName('mInput');
    for (var i2 = 0; i2 < mStyleForInput.length; i2++) {
        mStyleForInput[i2].style.width = '100px';
        // mStyleForInput[i2].style.autocomplete = 'false';
        mStyleForInput[i2].setAttribute('autocomplete', 'off');
        mStyleForInput[i2].style.float = 'right';
        mStyleForInput[i2].style.marginRight = '50px';
        mStyleForInput[i2].style.fontFamily = mFontFamily;
        mStyleForInput[i2].style.fontSize = '14px';
    }
    var mStyleForBtn = document.getElementsByClassName('mBtn');
    for (var i3 = 0; i3 < mStyleForBtn.length; i3++) {
        mStyleForBtn[i3].style.backgroundColor = '#fff'; // 淡色 #7fa8b0
        mStyleForBtn[i3].style.position = 'absolute';
        mStyleForBtn[i3].style.height = '50px';
        mStyleForBtn[i3].style.width = '50px';
        // mStyleForBtn[i3].style.left = '40px';
        mStyleForBtn[i3].style.top = '235px';
        mStyleForBtn[i3].style.borderRadius = '25px';
        mStyleForBtn[i3].style.boxShadow = '0px 0px 5px #333';
        mStyleForBtn[i3].style.cursor = 'pointer';
        mStyleForBtn[i3].style.zIndex = '9999999';
        // mStyleForBtn[i3].innerHTML = '填写';
        mStyleForBtn[i3].style.textAlign = 'center';
        mStyleForBtn[i3].style.color = '#333'; // 灰色
        mStyleForBtn[i3].style.lineHeight = '50px'; // 灰色
        mStyleForBtn[i3].style.userSelect = 'none'; // 不能选中按钮里面的文字

        mStyleForBtn[i3].addEventListener('mouseenter', function() {
            this.style.color = '#fff';
            this.style.backgroundColor = '#54565a'; // 灰色
            this.style.border = 'none';
        })
        mStyleForBtn[i3].addEventListener('mouseleave', function() {
            this.style.backgroundColor = '#fff';
            this.style.color = '#333';
        })
        mStyleForBtn[i3].addEventListener('mousedown', function() {
            this.style.backgroundColor = mColor; // 深色
        })
        mStyleForBtn[i3].addEventListener('mouseup', function() {
            this.style.backgroundColor = '#54565a'; // 灰色
        })
    }
    var mStyleForCheck = document.getElementsByClassName('mCheck');
    for (var i4 = 0; i4 < mStyleForCheck.length; i4++) {
        mStyleForCheck[i4].style.position = 'absolute';
        mStyleForCheck[i4].style.top = '5px';
        mStyleForCheck[i4].style.right = '30px';
        mStyleForCheck[i4].style.fontSize = '10px';
        mStyleForCheck[i4].style.height = '15px';
        mStyleForCheck[i4].style.width = '15px';
        mStyleForCheck[i4].style.lineHeight = '15px';
        mStyleForCheck[i4].style.textAlign = 'center';
        mStyleForCheck[i4].style.fontWeight = '700';

    }
    var mStyleForCheckCorrect = document.getElementsByClassName('mCorrect');
    for (var i5 = 0; i5 < mStyleForCheckCorrect.length; i5++) {
        mStyleForCheckCorrect[i5].style.backgroundColor = '#99cc66';
        mStyleForCheckCorrect[i5].style.borderRadius = '20px';
        mStyleForCheckCorrect[i5].style.color = '#fff';
        mStyleForCheckCorrect[i5].innerHTML = '√';
        mStyleForCheckCorrect[i5].style.mFontFamily = 'Candara';
        mStyleForCheckCorrect[i5].style.cursor = 'pointer';
        mStyleForCheckCorrect[i5].style.userSelect = 'none'; // 不能选中按钮里面的文字
    }
    var mStyleForCheckError = document.getElementsByClassName('mError');
    for (var i6 = 0; i6 < mStyleForCheckError.length; i6++) {
        mStyleForCheckError[i6].style.backgroundColor = '#cc0001';
        mStyleForCheckError[i6].style.borderRadius = '20px';
        mStyleForCheckError[i6].style.color = '#fff';
        mStyleForCheckError[i6].style.mFontFamily = 'Candara';
        mStyleForCheckError[i6].innerHTML = '×';
        mStyleForCheckError[i6].style.cursor = 'pointer';
        mStyleForCheckError[i6].style.userSelect = 'none'; // 不能选中按钮里面的文字
    }
    var mStyleForMsg = document.getElementsByClassName('msg');
    for (var i7 = 0; i7 < mStyleForMsg.length; i7++) {
        mStyleForMsg[i7].style.display = 'none'; // 开启时先隐藏
        // mStyleForMsg[i7].style.display = 'inline-block';
        mStyleForMsg[i7].style.position = 'absolute';
        // mStyleForMsg[i7].style.height = '70px';
        // mStyleForMsg[i7].style.width = 'auto'; // 宽度随内容变化而变化
        mStyleForMsg[i7].style.top = (window.innerHeight - 70) / 2 + 'px';
        mStyleForMsg[i7].style.left = (window.innerWidth - mStyleForMsg[i7].offsetWidth) / 2 + 'px';
        mStyleForMsg[i7].style.backgroundColor = '#fff';
        mStyleForMsg[i7].style.borderRadius = '25px'
        mStyleForMsg[i7].style.boxShadow = '0px 0px 5px #333'
        mStyleForMsg[i7].style.zIndex = '9999999';
        mStyleForMsg[i7].style.fontFamily = mFontFamily;
        mStyleForMsg[i7].style.opacity = '0'; // 开始透明
    }
    var mStyleForIcon = document.getElementsByClassName('icon');
    for (var i8 = 0; i8 < mStyleForIcon.length; i8++) {
        mStyleForIcon[i8].style.display = 'inline-block';
        mStyleForIcon[i8].style.height = '30px';
        mStyleForIcon[i8].style.width = '30px';
        mStyleForIcon[i8].style.lineHeight = '30px';
        mStyleForIcon[i8].style.textAlign = 'center';
        mStyleForIcon[i8].style.color = '#fff';
        mStyleForIcon[i8].style.fontWeight = '700';
        mStyleForIcon[i8].style.borderRadius = '15px'
        mStyleForIcon[i8].style.mFontFamily = 'Candara';
        mStyleForIcon[i8].style.marginLeft = '50px';
    }
    var mStyleForIconCorrect = document.getElementsByClassName('iconCorrect');
    for (var i9 = 0; i9 < mStyleForIconCorrect.length; i9++) {
        mStyleForIconCorrect[i9].style.backgroundColor = '#99cc66';
        mStyleForIconCorrect[i9].innerHTML = '√'
    }
    var mStyleForIconError = document.getElementsByClassName('iconError');
    for (var i10 = 0; i10 < mStyleForIconError.length; i10++) {
        mStyleForIconError[i10].style.backgroundColor = '#cc0001';
        mStyleForIconError[i10].innerHTML = '×'
    }












    // 版本信息
    var openInSetting = true; // 是否在设置里打开，如果是，背景cover就不消失
    if (getData('version') != version) {
        // 更新版本显示更新内容
        openInSetting = false;
        showVersionBox(true);
        addData('version', version);
    }






    // 下边是函数
    // 下边是函数
    // 下边是函数
    // 下边是函数
    // 下边是函数
    function autoFill() {
        var system = companyCodeBox.length ? 'SAP' : 'non-SAP';

        if (system == 'SAP') {

            companyCode = mCompanyCodeBox.value;
            if (mDocumentType1.checked) {
                documentType = 'SB';
                reversalDate = '';
            } else {
                documentType = 'Z1';
                reversalDate = mReversalDateBox.value;
            };
            postingDate = mPostingDateBox.value;
            longText = mLongTextBox.value;
            shortText = longText.substring(0, 16);

            companyCodeBox.val(companyCode);
            documentTypeBox.val(documentType);
            documentCurrencyBox.val(documentCurrency);
            preparerCostCenterBox.val(preparerCostCenter);
            referenceBox.val(shortText);
            adHocJudgmentalBox.val(shortText);

            postingDateBox.val(postingDate);
            documentDateBox.val(postingDate);
            translationDateBox.val(postingDate);
            reversalDateBox.val(reversalDate);
            controlTotalBox.val(controlTotal);
            jeDescriptionBox.val(longText);

            ctDescriptionBox.prop("readonly", ""); // 填写CT
            ctDescriptionBox.val(ctMapping[controlTotal]);
            ctDescriptionBox.prop("readonly", "readonly");

            ownershipBox.prop("readonly", ""); // 填写ownership
            ownershipBox.val(ownership);
            ownershipBox.prop("readonly", "readonly");

            // 把当前数据保存到localStorage
            addData('companyCode', companyCode);
            addData('preparerCostCenter', preparerCostCenter);
            addData('postingDate', postingDate);

        } else if (system != 'SAP') {
            companyCode = mCompanyCodeBox.value; // 这里就是nonSAP Location
            postingDate = mPostingDateBox.value;
            longText = mLongTextBox.value;
            shortText = longText.substring(0, 14); // 这里就是Document No - 14个字符

            nonSAPLocationBox.val(companyCode);
            postingDateBox.val(postingDate);
            entryCurrencyBox.val(documentCurrency);
            jeDescriptionBox.val(longText);
            documentNoBox.val(shortText);

            // 把当前数据保存到localStorage
            addData('companyCode', companyCode);
            addData('preparerCostCenter', preparerCostCenter);
            addData('postingDate', postingDate);
        }
    }


    function autoClear() {
        var system = companyCodeBox ? 'SAP' : 'non-SAP';
        // var cfmAutoClear = confirm('检测到当前的Template是：' + system + '，是否清空所有内容？');

        if (system == 'SAP') {
            companyCodeBox.val("");
            documentTypeBox.val("");
            documentCurrencyBox.val("");
            preparerCostCenterBox.val("");
            referenceBox.val("");
            adHocJudgmentalBox.val("");

            postingDateBox.val("");
            documentDateBox.val("");
            translationDateBox.val("");
            reversalDateBox.val("");
            controlTotalBox.val("");
            jeDescriptionBox.val("");
        } else if (system != 'SAP') {
            nonSAPLocationBox.val("");
            postingDateBox.val("");
            entryCurrencyBox.val("");
            jeDescriptionBox.val("");
            documentNoBox.val("");
        }
    }



    // 选择SB还是Z1
    function selectDocumentType(whichType) {
        if (whichType == 'SB') {
            mReversalDateBox.value = '';
            mReversalDateBox.disabled = 'true';
        } else {
            var date = postingDate.split('/');
            var reversalYear = 0;
            var reversalMonth = 0;
            var postingYear = parseInt(date[0]);
            var postingMonth = parseInt(date[1]);
            if (postingMonth >= 12) {
                reversalMonth = 1;
                reversalYear = postingYear + 1;
            } else {
                reversalMonth = postingMonth + 1;
                reversalYear = postingYear;
            }

            reversalDate = reversalYear + '/' + reversalMonth + '/' + reversalDay;

            mReversalDateBox.value = reversalDate;
            mReversalDateBox.disabled = '';
        }
        checkAll();
    }



    // 获取国家币种 - 如果没有就返回USD
    function mapCurrency(whichContry) {
        if (currencyMapping[whichContry] == undefined) {
            return 'USD';
        } else {
            return currencyMapping[whichContry];
        }
    }


    // 设置对错Style
    function setCheckCSS(checkObject) {
        if (checkObject.className.search('mCorrect') != -1) {
            checkObject.style.backgroundColor = '#99cc66';
            checkObject.innerHTML = '√';
        } else {
            checkObject.style.backgroundColor = '#cc0001';
            checkObject.innerHTML = '×';
        }
        checkObject.style.borderRadius = '20px';
        checkObject.style.color = '#fff';
        checkObject.style.mFontFamily = 'Candara';
        checkObject.style.display = 'block';
    }


    // Check所有的项目
    function checkAll() {
        mError = 0; // 初始化

        // CompanyCode 
        var mCurr = currencyMapping[companyCode];
        var companyCodeExsit = mCurr === undefined ? false : true; // 看国家是否存在 - 根据是否能匹配到货币
        mCheckCompanyCode.className = companyCodeExsit ? 'mText mCheck mCorrect' : 'mText mCheck mError';
        if (!companyCodeExsit) { mError += 1 };
        setCheckCSS(mCheckCompanyCode);
        // Currency
        var mCurry = mCurrencyBox.value;
        mCheckCurrency.className = mCurry.length == 3 ? 'mText mCheck mCorrect' : 'mText mCheck mError';
        if (mCurry.length != 3) { mError += 1 };
        setCheckCSS(mCheckCurrency);
        // PostingDate 验证是否日期格式
        var d = new Date(postingDate);
        var isDate = (d.getDate() == postingDate.substring(postingDate.length - 2));
        mCheckPostingDate.className = isDate ? 'mText mCheck mCorrect' : 'mText mCheck mError';
        if (!isDate) { mError += 1 };
        setCheckCSS(mCheckPostingDate);
        // ReversalDate 验证是否日期格式
        if (documentType != 'SB') {
            var dd = new Date(reversalDate);
            var isDate2 = (dd.getDate() == reversalDate.substring(reversalDate.length - 2));
            mCheckReversalDate.className = isDate2 ? 'mText mCheck mCorrect' : 'mText mCheck mError';
            if (!isDate2) { mError += 1 };
            setCheckCSS(mCheckReversalDate);
        } else {
            mCheckReversalDate.className = 'mText mCheck';
            mCheckReversalDate.style.display = 'none';
        }
    }


    // 重置面板
    function autoReset() {
        companyCode = getData('companyCode');
        companyCode = companyCode ? companyCode : '5003';
        mapCurrency(companyCode);
        postingDate = getData('postingDate');
        postingDate = postingDate ? postingDate : '2020/7/31';
        selectDocumentType(documentType);
        mCompanyCodeBox.value = companyCode;
        documentCurrency = mapCurrency(companyCode);
        mCurrencyBox.value = documentCurrency;
        mPostingDateBox.value = postingDate;
        mReversalDateBox.value = reversalDate;
        mLongTextBox.value = longText;
        checkAll();
        showMsg('重置完成。', true);
    }



    function showMsg(message, isOK, callback) {
        clearInterval(msgBox.timer);
        msg.innerHTML = message;
        var orgLeft = (window.innerWidth - parseInt(msgBox.css("width"))) / 2;
        msgBox.css({
            // transition: "opacity 0.5s,top 0.5s", // 动画延迟 不需要了
            display: "block",
            position: "fixed",
            opacity: 0,
            top: (window.innerHeight - 70) / 2 + 20,
            left: orgLeft,
            zIndex: 999999999,
        });
        // msgBox.style.top = (window.innerHeight - 70) / 2 - 20 + 'px;
        if (isOK) {
            msgIcon.className = 'icon iconCorrect';
            msgIcon.style.backgroundColor = '#99cc66';
            msgIcon.innerHTML = '√'
        } else {
            msgIcon.className = 'icon iconError';
            msgIcon.style.backgroundColor = '#cc0001';
            msgIcon.innerHTML = '×'
        }

        msgBox.animate({
            top: (window.innerHeight - 70) / 2,
            opacity: 1, // 透明度变1
        }, 500); // 动画效果 500毫秒

        clearInterval(msgBox.timer); // 先清除定时器，只能有一个定时器执行。
        msgBox.timer = setInterval(function() {
            if (mWaitTime > 1) {
                mWaitTime--;
            } else {
                msgBox.animate({
                    top: (window.innerHeight - 70) / 2 - 20,
                    opacity: 0, // 透明度变1
                }, 500, "swing", function() {
                    msgBox.css("display", "none");
                }); // 动画效果 500毫秒

                clearInterval(msgBox.timer);
                mWaitTime = mMsgDisappearTime; // 初始化消息框消失时间
                if (callback) {
                    callback(); // 回调函数
                };
            }
        }, 1000);
    }



    function showConfirm(message, whatToDo) {
        confirmMsg.innerHTML = message;
        var orgTop = (window.innerHeight - parseInt(confirmBox.css("height"))) / 2;
        var orgLeft = (window.innerWidth - parseInt(confirmBox.css("width"))) / 2;
        // confirmBox.style.top = (window.innerHeight - confirmBox.offsetHeight) / 2 + 20 + 'px';
        // confirmBox.style.left = (window.innerWidth - confirmBox.offsetWidth) / 2 + 'px';

        confirmBox.css({
            position: "fixed",
            display: "block",
            top: orgTop + 20,
            left: orgLeft,
        });

        confirmIcon.style.marginLeft = '100px';
        confirmIcon.style.backgroundColor = mColor;
        confirmIcon.innerHTML = '?';
        btnConfirm.style.top = '90px';
        btnCancel.style.top = '90px';

        confirmBox.animate({
            opacity: 1,
            top: orgTop,
        }, 200);

        cover.style.display = 'block';
        cover.style.transition = 'opacity 0.5s';
        cover.style.opacity = '.3';

        doWhat = whatToDo;
    }



    // localStorage操作

    function loadData() {
        // 清除掉1.0版本下的数据
        localStorage.removeItem('companyCode');
        localStorage.removeItem('postingDate');
        localStorage.removeItem('preparerCostCenter');

        if (allData) {
            var dataArr = allData.split(',');
            var eachData = [];
            for (var i = 0; i < dataArr.length; i++) {
                eachData = dataArr[i].split(':');
                dataDic[eachData[0]] = eachData[1];
                eachData = [];
            }
        }
        console.log(dataDic);
    }

    function addData(key, value) {
        dataDic[key] = value;
        saveData();
    }

    function getData(key) {
        if (key in dataDic) {
            return dataDic[key];
        } else {
            return undefined;
        }
    }

    function removeData(key) {
        delete dataDic[key];
        saveData();
    }

    function saveData() {
        var dataStr = JSON.stringify(dataDic);
        dataStr = dataStr.substring(1, dataStr.length - 1) // 去掉{}
        dataStr = dataStr.replace(/"/g, ''); // 去掉"  /正则表达式/g = global = 所有的
        localStorage.setItem('mhData', dataStr);
    }





    // 自动识别CompanyCode 
    function identifyCompanyCode() {
        var text = nameBox.val(); // JE名称
        var keyArr = Object.keys(currencyMapping); // 生成一个全是key的数组
        for (var i = 0; i < keyArr.length; i++) {
            if (text.indexOf(keyArr[i]) != -1) {
                return keyArr[i];
            }
        }
        if (text.indexOf('0215' != -1)) {
            return '5002';
        };
        return null;
    }




    // 打开设置面板
    var settingFormat = true;
    var selectedTab = 0; // 当前选中的Tab

    function showSetting(tabIndex) {
        // 第一次打开渲染
        if (settingFormat) {
            // 左侧按钮
            var settingTabBtn = $(".settingTabBtn");
            for (var i = 0; i < settingTabBtn.length; i++) {
                $(settingTabBtn[i]).css({
                    display: "inline-block",
                    position: "absolute",
                    textAlign: "center",
                    lineHeight: 30 + "px",
                    height: 30,
                    width: 100,
                    left: 50,
                    top: 80 + (30 + 15) * i,
                    color: "#333",
                    fontWeight: 700,
                    borderRadius: "30px",
                    cursor: "pointer",
                });
                if ($(settingTabBtn[i]).html() == "") {
                    $(settingTabBtn[i]).css("display", "none"); // 隐藏没有字的
                }
            };
            $(".settingTabBtn_Float").css({
                top: 80 + (30 + 15) * tabIndex,
                display: "block",
                backgroundColor: mColor,
                zIndex: -1,
            });
            // 右侧面板
            var settingTab = $(".settingTab");
            for (var j = 0; j < settingTab.length; j++) {
                $(settingTab[j]).css({
                    display: "none",
                    position: "absolute",
                    height: 460 - 20 * 2,
                    width: 660 - 20 * 2,
                    left: 50 + 50 + 100,
                    top: 60,
                    borderRadius: "20px",
                    // backgroundColor: "rgb(241 241 241)",
                    padding: "20px 20px",
                    boxShadow: "0px 0px 3px #333",
                });
            };
            var tabLi = $(".tabLi");
            for (var ii = 0; ii < tabLi.length; ii++) {
                $(tabLi[ii]).css({
                    margin: 0,
                    padding: 0,
                    listStyle: "none",
                    fontSize: "12px",
                    height: 30,
                    lineHeight: "30px",
                });
            };
            settingFormat = false;
        }

        touchSettingTabBtn(tabIndex);
        selectSettingTabBtn(tabIndex);

        settingBox.css({
            top: (window.innerHeight - parseInt(settingBox.css("height"))) / 2 + 20,
            left: (window.innerWidth - parseInt(settingBox.css("width"))) / 2,
            display: "block",
            opacity: 0,
            position: "fixed",
        });

        cover.style.display = 'block';
        cover.style.opacity = '.3';

        $(settingBox).animate({
            opacity: 1,
            top: (window.innerHeight - parseInt(settingBox.css("height"))) / 2,
        }, 500);
    }

    function touchSettingTabBtn(index) {
        var settingTabBtn = $(".settingTabBtn");
        var mTop = $(settingTabBtn[index]).css("top");
        $(".settingTabBtn_Float").stop().animate({
            top: mTop,
        }, 200); // 用回调函数会出现，操作过快，字变白后变不回去的BUG
        $(settingTabBtn[index]).css({
            color: "#fff",
        });
    }

    function selectSettingTabBtn(index) {
        var settingTabBtn = $(".settingTabBtn");
        var settingTab = $(".settingTab");
        $(settingTabBtn[selectedTab]).css({
            color: "#333",
            backgroundColor: "",
        });
        $(settingTab[selectedTab]).css({
            display: "none",
        });
        $(settingTabBtn[index]).css({
            color: "#fff",
            backgroundColor: mColor,
        });
        $(settingTab[index]).css({
            display: "block",
        });
        selectedTab = index;
    }

    function leaveSettingTabBtn(index) {
        var settingTabBtn = $(".settingTabBtn");
        $(settingTabBtn[index]).css({
            color: "#333",
            backgroundColor: "none",
        });
        touchSettingTabBtn(selectedTab);
    }




    // 版本框操作
    function showVersionBox(isNewVersion) {

        if (isNewVersion) {
            versionTitle.html("脚本已更新 - 当前版本：" + version);
        } else {
            versionTitle.html("版本信息");
        }

        $(versionBox).css({
            top: (window.innerHeight - parseInt(versionBox.css("height"))) / 2 + 20,
            left: (window.innerWidth - parseInt(versionBox.css("width"))) / 2,
            display: "block",
            opacity: 0,
            position: "fixed",
            zIndex: 99999999999,
        });

        cover.style.display = 'block';
        cover.style.opacity = '.3';

        $(versionBox).animate({
            opacity: 1,
            top: (window.innerHeight - parseInt(versionBox.css("height"))) / 2,
        }, 500);


        $(versionHistortText).html('\
        <h1 style="font-family:Microsoft Yahei;color:rgb(0, 0, 0);font-size:20px;text-transform:none;">米黄Trintech脚本1.2.3</h1>\
        <p style="color:#777676;">发布日期 - 2020/9/1</p>\
        <h3>更新内容</h3>\
        <ul>\
        <li>h1标签优化：调整了脚本中的h1标签英文字母全是大写的问题。</li>\
        <li>设置面板优化：优化了跟随框的初始位置。刷新页面第一次打开设置面板时，跟随框不会从下至上一闪而过了。</li>\
        </ul>\
        <h3>BUG修复</h3>\
        <ul>\
        <li>修复了一个重置后，内存中的currency不变，导致填写的currency错误的问题。</li>\
        <li>修复了companyCode填写5018会出现错误的问题（currencyMapping5018错写成了5017）。</li>\
        </ul>\
        <br><br><br><br><br><br></br>\
        <h1 style="font-family:Microsoft Yahei;color:rgb(0, 0, 0);font-size:20px;text-transform:none;">米黄Trintech脚本1.2.2</h1>\
        <p style="color:#777676;">发布日期 - 2020/8/25</p>\
        <h3>BUG修复</h3>\
        <ul>\
        <li>修复了一个识别CompanyCode后，documentCurrency在主面板改变却没有在内存中改变，导致填写的信息currency错误的问题。</li>\
        </ul>\
        <br><br><br><br><br><br></br>\
        <h1 style="font-family:Microsoft Yahei;color:rgb(0, 0, 0);font-size:20px;text-transform:none;">米黄Trintech脚本1.2.1</h1>\
        <p style="color:#777676;">发布日期 - 2020/8/24</p>\
        <h3>新增内容</h3>\
        <ul>\
        <li>现在，在加载页面时，可以自动填写信息。这个功能默认不启用，可以在<strong>设置面板>基础>程序</strong>中进行设置。</li>\
        </ul>\
        <h3>更新内容</h3>\
        <ul>\
        <li>自动识别CompanyCode：原本0215不可识别，现在可以将0215识别为5002。</li>\
        </ul>\
        <br><br><br><br><br><br></br>\
        <h1 style="font-family:Microsoft Yahei;color:rgb(0, 0, 0);font-size:20px;text-transform:none;">米黄Trintech脚本1.2.0</h1>\
        <p style="color:#777676;">发布日期 - 2020/8/24</p>\
        <h3>新增内容</h3>\
        <ul>\
        <li>程序名称由插件改为了<strong>脚本</strong>，因为这本身就属于脚本程序。</li>\
        <li>设置面板 - 基本：现在可以设置主题颜色了，并且可以存入localStorage，参数在下次打开时依然保留。</li>\
        <li>设置面板 - 关于：现在可以随时查看不同时期的版本信息了，并可以主动检查更新。</li>\
        <li>设置面板使用技巧：双击设置标签可以恢复程序默认参数。</li>\
        <li>后期会增加更多有用的参数在设置面板当中。</li>\
        </ul>\
        <h3>更新内容</h3>\
        <ul>\
        <li>在打开页面时，会自动从JE Name中识别companyCode填入主面板，并同时保存到localStorage中。（如果没有可识别的CompanyCode，将使用localStorage中的参数，如果localStorage中也没有，将使用脚本默认CompanyCode。）</li>\
        <li>JE Description在自动生成后，会被存入内存当中。因此现在重置按钮可以重置JE Description了。</li>\
        </ul>\
        <br><br><br><br><br><br></br>\
        <h1 style="font-family:Microsoft Yahei;color:rgb(0, 0, 0);font-size:20px;text-transform:none;">米黄Trintech插件1.1.2</h1>\
        <p style="color:#777676;">发布日期 - 2020/8/20</p>\
        <h3>更新内容</h3>\
        <ul>\
        <li>创建JE后，JE Description内容将自动填写为JE Name的内容。</li>\
        </ul>\
        <br><br><br><br><br><br></br>\
        <h1 style="font-family:Microsoft Yahei;color:rgb(0, 0, 0);font-size:20px;text-transform:none;">米黄Trintech插件1.1.1</h1>\
        <p style="color:#777676;">发布日期 - 2020/8/18</p>\
        <h3>更新内容</h3>\
        <ul>\
        <li>将确认框的出现和消失动画延迟响应时间由500毫秒缩短到200毫秒。</li>\
        <li>确认框确认后也会出现程序执行完成的确认框。</li>\
        </ul>\
        <h3>BUG修复</h3>\
        <ul>\
        <li>确认框修改为jQuery语法，修复了消失动画错误的BUG。</li>\
        <li>修复了主面板无法移动的BUG。</li>\
        <li>修复了提示框水平居中位置不变的BUG。</li>\
        </ul>\
        <br><br><br><br><br><br></br>\
        <h1 style="font-family:Microsoft Yahei;color:rgb(0, 0, 0);font-size:20px;text-transform:none;">米黄Trintech插件1.1.0</h1>\
        <p style="color:#777676;">发布日期 - 2020/8/17</p>\
        <h3>更新内容</h3>\
        <ul>\
        <li>引用jQuery库，替换了部分原生JS代码，增加了动画显示的准确性。</li>\
        <li>重写localStorage数据存储逻辑，所有数据只需要保存到一个key当中 - mhData</li>\
        <li>新增版本更新框，仅在每次版本更新时自动弹出。预计未来可以在设置中重新查看。</li>\
        </ul>\
        <h3>BUG修复</h3>\
        <ul>\
        <li>修复了透明提示框挡住网页内容的BUG。</li>\
        </ul>\
        <h3>已知BUG</h3>\
        <ul>\
        <li>确认框在关闭时不能正确得显示上移的动画。</li>\
        <li>主板无法移动。</li>\
        </ul>\
        <br><br><br><br><br><br></br>\
        <h1 style="font-family:Microsoft Yahei;color:rgb(0, 0, 0);font-size:20px;text-transform:none;">米黄Trintech插件1.0.0</h1>\
        <p style="color:#777676;">发布日期 - 2020/8/11</p>\
        <h3>主要功能</h3>\
        <ul>\
        <li>填写：自动填写除借贷以外的所有数据。同时支持SAP和non-SAP</li>\
        <li>清空：自动清空除借贷以外的所有数据。同时支持SAP和non-SAP</li>\
        <li>重置：将主面板的数据初始化。</li>\
        </ul>\
        <h3>数据存储</h3>\
        <ul>\
        <li>JE相关数据会在确认填写时保存在浏览器（localStorage）之中。</li>\
        <li>JE相关的数据包含：conpanyCode，postingDate和preparerCostCenter。</li>\
        <li>还有一些设置相关的数据之后会在设置功能开放后可以使用以及保存。包含但不限于：颜色，保存移动位置，设置CC，提示框消失时间，填写清空是否提示，等等。</li>\
        </ul>\
        <h3>其他</h3>\
        <ul>\
        <li>主面板可以移动，但刷新页面后会回到原先的默认位置。将在之后的设置功能中可手动设置。</li>\
        <li>主面板的currency和reversalDate会根据conpanyCode和postingDate自动计算。</li>\
        </ul>\
        <h3>已知BUG</h3>\
        <ul>\
        <li>消息框可能会造成框框消失后，在消失前的区域鼠标无法点击到后面的内容。</li>\
        </ul>\
        ');
    }



})();